package com.topc.code.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.topc.base.exception.Asserts;
import com.topc.base.model.PageResult;
import com.topc.base.utils.PageUtils;
import com.topc.code.mapper.ProblemUserMidMapper;
import com.topc.code.repository.ProblemCommitStatusCollectRepository;
import com.topc.code.service.IProblemContentCommitService;
import com.topc.code.service.IProblemUserMidService;
import com.topc.code.service.feign.CompetitionClient;
import com.topc.core.sevice.RedisService;
import com.topc.problem.model.dto.QueryProblemUserMidDto;
import com.topc.problem.model.po.*;
import com.topc.problem.model.vo.ProblemScoreListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProblemUserMidServiceImpl extends ServiceImpl<ProblemUserMidMapper, ProblemUserMid> implements IProblemUserMidService {

    @Autowired
    private ProblemCommitStatusCollectRepository problemCommitStatusCollectRepository;

    @Autowired
    private IProblemContentCommitService problemContentCommitService;

    @Autowired
    RedisService redisService;

    @Autowired
    private IProblemUserMidService problemUserMidService;

    @Autowired
    private ProblemUserMidMapper problemUserMidMapper;

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    private CompetitionClient competitionClient;

    @Override
    public PageResult<ProblemUserMid> searchByPage(int pageSize, int pageCurrent, QueryProblemUserMidDto queryProblemUserMidDto) {
        LambdaQueryWrapper<ProblemUserMid> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(queryProblemUserMidDto.getProblemId()),ProblemUserMid::getProblemId,queryProblemUserMidDto.getProblemId())
                .eq(ObjectUtil.isNotEmpty(queryProblemUserMidDto.getUserNumber()),ProblemUserMid::getUserNumber,queryProblemUserMidDto.getUserNumber())
                .eq(ObjectUtil.isNotEmpty(queryProblemUserMidDto.getCompetitionId()),ProblemUserMid::getCompetitionId,queryProblemUserMidDto.getCompetitionId());
        return PageUtils.pageToResult(this.getBaseMapper().selectPage(PageUtils.toPageBuild(pageSize,pageCurrent),lambdaQueryWrapper));
    }

    @Override
    public boolean insertCommit(ProblemUserMid problemUserMid) {
        ProblemCommitStatusCollect qcsc = new ProblemCommitStatusCollect();
        qcsc.setCommitStatus(problemUserMid.getCommitStatus());
        ProblemCommitStatusCollect saveOne = problemCommitStatusCollectRepository.save(qcsc);
        if (ObjectUtil.isEmpty(saveOne)) return false;
        problemUserMid.setConditionMid(saveOne.getId());
        ProblemContentCommit entity = new ProblemContentCommit();
        entity.setContent(problemUserMid.getContent()).setCodeType(problemUserMid.getCodeType());
        boolean save = problemContentCommitService.save(entity);
        if (!save){
            problemCommitStatusCollectRepository.deleteById(saveOne.getId());
            return false;
        }
        problemUserMid.setContentId(entity.getId());
        return this.save(problemUserMid);
    }

    //是否存在提交信息
    @Override
    public boolean ifExistInfo(Long problemId, String userNumber,Long competitionId) {
        LambdaQueryWrapper<ProblemUserMid> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProblemUserMid::getProblemId,problemId)
                .eq(ProblemUserMid::getUserNumber,userNumber)
                .eq(ProblemUserMid::getStatus,0)
               .eq(ProblemUserMid::getCompetitionId,competitionId);
        int count = problemUserMidService.count(queryWrapper);
        if (count == 0){
            //用户不存在未运行完的提交程序
            return false;
        }
        return true;
    }

    @Override
    public void MongodbTest() {
        ProblemSampleCollect problemSampleCollect = new ProblemSampleCollect();
        problemSampleCollect.setId("13");
        ProblemSample problemSample = new ProblemSample();
        problemSample.setInput("13");
        problemSample.setOutput("13");
        List<ProblemSample> problemSampleList=new ArrayList<>();
        problemSampleList.add(problemSample);
        problemSampleCollect.setSamples(problemSampleList);
        System.out.println("TestData"+problemSampleList);
        mongoTemplate.save(problemSampleCollect);
    }

    @Override
    public void updateCompetitionScore(ProblemUserMid problemUserMid) {
        String userNumber = problemUserMid.getUserNumber();
        Long competitionId = problemUserMid.getCompetitionId();
        System.out.println("info"+userNumber+"////////////////////"+competitionId);
        int score=0;
        //查询竞赛结果记录(分数排名)
        List<ProblemScoreListVo> competitionScoreList =problemUserMidMapper.getCompetitionScoreList(competitionId,userNumber);
        System.out.println("comList++++++++++++++++++"+competitionScoreList);
        for (ProblemScoreListVo competitionScore:competitionScoreList){
            score+=competitionScore.getScore();
        }
        System.out.println("理论总分:"+score);
        //更新总分(找到参赛信息-->userNumber,competitionId,分数差值)
        boolean update = competitionClient.updateScore(userNumber, competitionId, score);
        if (!update){
            Asserts.fail("更新总分失败");
        }
    }
}
